//
//  String + Extension.swift
//  正则获取位置
//
//  Created by 曹晓龙 on 15/11/4.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import Foundation


extension String {
    
    func href() -> (link:String, text:String)? {
        
        let pattern = "<a href=\"(.*?)\" .*?\">(.*?)</a>"
        
        let regex = try! NSRegularExpression(pattern: pattern, options: [])
        
        guard let result = regex.firstMatchInString(self, options: [], range: NSRange(location: 0, length: characters.count)) else{
            
            return nil
        }
        
        let r1 = result.rangeAtIndex(1)
        
        let link = (self as NSString).substringWithRange(r1)
        
        let r2 = result.rangeAtIndex(2)
        
        let text = (self as NSString).substringWithRange(r2)
        
  
        return (link, text)
    }
    
}